#include "Logger.h"

namespace SeanNetwork {

Logger::Logger() 
    : m_Initialized(false), 
      m_LogLevel(LogLevel::Info), 
      m_LogToFile(false) 
{
}

bool Logger::Initialize(LogLevel logLevel, bool logToFile, const std::string& logFile) {
    m_LogLevel = logLevel;
    m_LogToFile = logToFile;
    
    if (m_LogToFile && !logFile.empty()) {
        m_LogFilePath = logFile;
        try {
            m_LogFile.open(m_LogFilePath, std::ios::out | std::ios::app);
            if (!m_LogFile.is_open()) {
                std::cerr << "无法打开日志文件: " << m_LogFilePath << std::endl;
                m_LogToFile = false;
                return false;
            }
        }
        catch (const std::exception& e) {
            std::cerr << "打开日志文件异常: " << e.what() << std::endl;
            m_LogToFile = false;
            return false;
        }
    }
    
    m_Initialized = true;
    Log(LogLevel::Info, "日志系统初始化完成");
    return true;
}

void Logger::Shutdown() {
    if (!m_Initialized) return;
    
    std::lock_guard<std::mutex> lock(m_Mutex);
    if (m_LogToFile && m_LogFile.is_open()) {
        m_LogFile.flush();
        m_LogFile.close();
    }
    
    m_Initialized = false;
}

void Logger::SetLogLevel(LogLevel level) {
    m_LogLevel = level;
}

LogLevel Logger::GetLogLevel() const {
    return m_LogLevel;
}

void Logger::SetCallback(LogCallback callback) {
    std::lock_guard<std::mutex> lock(m_Mutex);
    m_Callback = callback;
}

void Logger::Log(LogLevel level, const std::string& message) {
    if (!m_Initialized || level < m_LogLevel) return;
    
    // 获取当前时间
    auto now = std::chrono::system_clock::now();
    auto now_time = std::chrono::system_clock::to_time_t(now);
    auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
    
    // 格式化日志
    std::stringstream ss;
    ss << "[" << std::put_time(std::localtime(&now_time), "%Y-%m-%d %H:%M:%S") 
       << "." << std::setfill('0') << std::setw(3) << ms.count() << "] "
       << "[" << LogLevelToString(level) << "] "
       << "[Thread-" << std::this_thread::get_id() << "] "
       << message;
    
    std::string logMessage = ss.str();
    
    std::lock_guard<std::mutex> lock(m_Mutex);
    
    // 控制台输出
    if (level >= LogLevel::Warning) {
        std::cerr << logMessage << std::endl;
    } else {
        std::cout << logMessage << std::endl;
    }
    
    // 文件输出
    if (m_LogToFile && m_LogFile.is_open()) {
        m_LogFile << logMessage << std::endl;
        m_LogFile.flush();
    }
    
    // 回调输出
    if (m_Callback) {
        m_Callback(level, logMessage);
    }
}

void Logger::Debug(const std::string& message) {
    Log(LogLevel::Debug, message);
}

void Logger::Info(const std::string& message) {
    Log(LogLevel::Info, message);
}

void Logger::Warning(const std::string& message) {
    Log(LogLevel::Warning, message);
}

void Logger::Error(const std::string& message) {
    Log(LogLevel::Error, message);
}

void Logger::Fatal(const std::string& message) {
    Log(LogLevel::Fatal, message);
}

std::string Logger::LogLevelToString(LogLevel level) {
    switch (level) {
        case LogLevel::Debug:   return "DEBUG";
        case LogLevel::Info:    return "INFO";
        case LogLevel::Warning: return "WARN";
        case LogLevel::Error:   return "ERROR";
        case LogLevel::Fatal:   return "FATAL";
        default:                return "UNKNOWN";
    }
}

} // namespace SeanNetwork 